<?php
class KehoachnvsController extends AppController {

	var $name = 'Kehoachnvs';

	function index() {
		$this->Kehoachnv->recursive = 0;
		$this->set('kehoachnvs', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid kehoachnv', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('kehoachnv', $this->Kehoachnv->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Kehoachnv->create();
			if ($this->Kehoachnv->save($this->data)) {
				$this->Session->setFlash(__('The kehoachnv has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The kehoachnv could not be saved. Please, try again.', true));
			}
		}
		$thoigians = $this->Kehoachnv->Thoigian->find('list');
		$nhanviens = $this->Kehoachnv->Nhanvien->find('list');
		$this->set(compact('thoigians', 'nhanviens'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid kehoachnv', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Kehoachnv->save($this->data)) {
				$this->Session->setFlash(__('The kehoachnv has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The kehoachnv could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Kehoachnv->read(null, $id);
		}
		$thoigians = $this->Kehoachnv->Thoigian->find('list');
		$nhanviens = $this->Kehoachnv->Nhanvien->find('list');
		$this->set(compact('thoigians', 'nhanviens'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for kehoachnv', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Kehoachnv->delete($id)) {
			$this->Session->setFlash(__('Kehoachnv deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Kehoachnv was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}

	function giaochitieu($nhanvien_id=null)
    {
		$nam = $this->Kehoachnv->find('list',array(
	    										'conditions'=>array('nhanvien_id'=>$nhanvien_id),
	    										'fields'=>array('ngaybd'),
	    										'group'=>array('YEAR(Kehoachnv.ngaybd)'),
	    										'order'=>array('ngaybd DESC')));
    		foreach ($nam as $key => $value) 
    		{
    			$nams[date('Y',strtotime($value))]=date('Y',strtotime($value)); 
    		}
    	$namht = date('Y');	   
	    $tennvs=$this->Kehoachnv->query("select tennv from nhanviens where nhanviens.id=".$nhanvien_id);
		$this->Kehoachnv->recursive = 0;
		$this->paginate = array('order' => array('Kehoachnv.ngaybd'=>'DESC'),'limit' => 100);
		$this->set('kehoachthangs', $this->paginate(array('nhanvien_id'=>$nhanvien_id,'DATEDIFF(Kehoachnv.ngaykt,Kehoachnv.ngaybd) >='=>7,'YEAR(Kehoachnv.ngaybd)'=>$namht)));
        $this->paginate = array('order' => array('Kehoachnv.ngaybd'=>'DESC'),'limit' => 100);
		$this->set('kehoachtuans', $this->paginate(array('nhanvien_id'=>$nhanvien_id,'DATEDIFF(Kehoachnv.ngaykt,Kehoachnv.ngaybd) <'=>7,'YEAR(Kehoachnv.ngaybd)'=>$namht)));
        $t=$this->Kehoachnv->find('list',array('conditions'=>array('nhanvien_id'=>$nhanvien_id)));
        $this->set(compact('nams','tennvs','madv','nhanvien_id'));
	}

	function laygiaochitieu($nhanvien_id=null)
	{
		$this->layout='ajax';
    	$namht = $this->data['Kehoachnvs']['nam'];	   
        $tennvs=$this->Kehoachnv->query("select tennv from nhanviens where nhanviens.id=".$nhanvien_id);
		$this->Kehoachnv->recursive = 0;
		$this->paginate = array('order' => array('Kehoachnv.ngaybd'=>'DESC'),'limit' => 100);
		$this->set('kehoachthangs', $this->paginate(array('nhanvien_id'=>$nhanvien_id,'DATEDIFF(Kehoachnv.ngaykt,Kehoachnv.ngaybd) >='=>7,'YEAR(Kehoachnv.ngaybd)'=>$namht)));
        $this->paginate = array('order' => array('Kehoachnv.ngaybd'=>'DESC'),'limit' => 100);
		$this->set('kehoachtuans', $this->paginate(array('nhanvien_id'=>$nhanvien_id,'DATEDIFF(Kehoachnv.ngaykt,Kehoachnv.ngaybd) <'=>7,'YEAR(Kehoachnv.ngaybd)'=>$namht)));
        $t=$this->Kehoachnv->find('list',array('conditions'=>array('nhanvien_id'=>$nhanvien_id)));
        $this->set(compact('nams','tennvs','madv','nhanvien_id'));
		
	}
	
}
